package com.rt.easyjava.base.modules.timerjob.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.base.modules.timerjob.bean.TmptTimerLogBiz;
import com.rt.easyjava.base.modules.timerjob.dto.TmptTimerLogBizDto;
import com.rt.easyjava.base.modules.timerjob.service.intf.TmptTimerLogBizService;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.exception.ControllerException;
import com.rt.easyjava.common.utils.others.DateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Method;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2020-06-01
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson)
 */
public class TmptTimerLogController extends ControllerException {
    private final Logger logger = LoggerFactory.getLogger(TmptTimerLogController.class);

    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected TmptTimerLogBizService tmptTimerLogBizService;


    protected Result page(TmptTimerLogBizDto tmptTimerLogBizDto){
        Result<TmptTimerLogBizDto> result = new Result<>();

        Page<TmptTimerLogBiz> page = new Page<>(tmptTimerLogBizDto.getPage(), tmptTimerLogBizDto.getLimit());

        TmptTimerLogBiz tmptTimerLogBiz = mapper.map(tmptTimerLogBizDto, TmptTimerLogBiz.class);
        if(tmptTimerLogBiz.getDeleteflag()==null){
            tmptTimerLogBiz.setDeleteflag(0);
        }

        QueryWrapper<TmptTimerLogBiz> queryWrapper=new QueryWrapper<>(tmptTimerLogBiz);
        //特殊查询条件
        queryWrapper.orderBy(tmptTimerLogBizDto.getCondition(), tmptTimerLogBizDto.isAsc(), tmptTimerLogBizDto.getColumn());

        return tmptTimerLogBizService.page(page, queryWrapper);
    }

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    protected Result tree (Long id) {
        Result<TmptTimerLogBizDto> result = new Result<>();
        // 树递归
        TmptTimerLogBiz tmptTimerLogBiz = tmptTimerLogBizService.getById(id);
        TmptTimerLogBizDto tmptTimerLogBizDto = mapper.map(tmptTimerLogBiz, TmptTimerLogBizDto.class);
        treeReduce(tmptTimerLogBizDto);
        result.setObj(tmptTimerLogBizDto);

        return result.success();
    }

    private void treeReduce(TmptTimerLogBizDto tmptTimerLogBizDto){
    }

    protected Result myPage(TmptTimerLogBizDto tmptTimerLogBizDto){
        Result<TmptTimerLogBizDto> result = new Result<>();

        Page<TmptTimerLogBiz> page = new Page<>(tmptTimerLogBizDto.getPage(), tmptTimerLogBizDto.getLimit());

        TmptTimerLogBiz tmptTimerLogBiz = mapper.map(tmptTimerLogBizDto, TmptTimerLogBiz.class);
        if(tmptTimerLogBiz.getDeleteflag()==null){
            tmptTimerLogBiz.setDeleteflag(0);
        }

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptTimerLogBiz> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptTimerLogBiz );
        //特殊查询条件
        queryWrapper.orderBy(tmptTimerLogBizDto.getCondition(), tmptTimerLogBizDto.isAsc(), "t1."+tmptTimerLogBizDto.getColumn());

        result = tmptTimerLogBizService.myPage(page, queryWrapper);

        return result;
    }

    protected Result selectBy(TmptTimerLogBizDto tmptTimerLogBizDto){
        Result<TmptTimerLogBizDto> result = new Result<>();

        TmptTimerLogBiz tmptTimerLogBiz = mapper.map(tmptTimerLogBizDto, TmptTimerLogBiz.class);
        if(tmptTimerLogBiz.getDeleteflag()==null){
            tmptTimerLogBiz.setDeleteflag(0);
        }

        QueryWrapper<TmptTimerLogBiz> queryWrapper=new QueryWrapper<>(tmptTimerLogBiz);
        //特殊查询条件
        queryWrapper.orderBy(tmptTimerLogBizDto.getCondition(), tmptTimerLogBizDto.isAsc(), tmptTimerLogBizDto.getColumn());

        result = tmptTimerLogBizService.selectBy(queryWrapper);

        return result;
    }

    protected Result selectById(Long id){
        //调用服务
        return tmptTimerLogBizService.selectById(id);
    }

    protected Result select4column(String columnName){
        try {
            Object o = SpringContextHolder.getBean("tmptTimerLogBizService");
            Method method = o.getClass().getMethod("mySelect4"+columnName);
            return Result.builder(method.invoke(o)).success();
        }catch (Exception e){
            throw new RuntimeException("方法名不存在");
        }
    }

    protected Result create(@RequestBody TmptTimerLogBizDto tmptTimerLogBizDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptTimerLogBizDto.setCreatemanId(Long.parseLong(_userId));
        tmptTimerLogBizDto.setCreateman(_userName);
        tmptTimerLogBizDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptTimerLogBizDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptTimerLogBizService.create(tmptTimerLogBizDto);
    }

    protected Result update(@RequestBody TmptTimerLogBizDto tmptTimerLogBizDto,
                        String _userId, String _userName){
        //参数
        tmptTimerLogBizDto.setModifymanId(Long.parseLong(_userId));
        tmptTimerLogBizDto.setModifyman(_userName);
        tmptTimerLogBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptTimerLogBizDto.setCreatedate(null);
        tmptTimerLogBizDto.setCreateman(null);
        tmptTimerLogBizDto.setCreatemanId(null);
        tmptTimerLogBizDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptTimerLogBizDto.getId()==null?"0":tmptTimerLogBizDto.getId().longValue());

        //调用服务
        return tmptTimerLogBizService.update(tmptTimerLogBizDto, updateWrapper);
    }

    protected Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptTimerLogBizDto tmptTimerLogBizDto = new TmptTimerLogBizDto();
        tmptTimerLogBizDto.setModifymanId(Long.parseLong(_userId));
        tmptTimerLogBizDto.setModifyman(_userName);
        tmptTimerLogBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptTimerLogBizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptTimerLogBizService.update(tmptTimerLogBizDto, updateWrapper);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptTimerLogBizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
